package cn.appsys.service.developer;

import cn.appsys.dao.appinfo.AppInfoMapper;
import cn.appsys.dao.appversion.AppVersionMapper;
import cn.appsys.pojo.AppInfo;
import cn.appsys.pojo.AppVersion;
import cn.appsys.util.PageSupport;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    public PageSupport<AppInfo> getPageSupport(Map<String, Object> params) {
        PageSupport<AppInfo> pageSupport=new PageSupport<AppInfo>();
        pageSupport.setCurrentPageNo((Integer) params.get("currentPageNo"));
        pageSupport.setPageSize((Integer)params.get("pageSize"));
        int start=(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize();
        params.put("start",start);
        pageSupport.setTotalCount(appInfoMapper.getAppInfoCount(params));
        pageSupport.setList(appInfoMapper.getAppInfoList(params));
        return pageSupport;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public AppInfo getAppInfo(Integer id) {
        return appInfoMapper.getAppInfo(id);
    }

    @Override
    public boolean updateAppInfo(AppInfo appInfo) {
        if (appInfoMapper.updateAppInfo(appInfo)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteAppLogo(Integer id) {
        if (appInfoMapper.deleteAppLogo(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean addAppInfo(AppInfo appInfo) {
        if (appInfoMapper.addAppInfo(appInfo)==1){
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public AppInfo getAppInfoByName(String APKName) {
        return appInfoMapper.getAppInfoByName(APKName);
    }

    @Override
    public boolean deleteApp(Integer id) {
        boolean flag=false;
        List<AppVersion> appVersionList = appVersionMapper.getAppVersionList(id);
        if (appVersionList.size()>0){
            for (AppVersion appVersion:appVersionList){
                String apkLocPath = appVersion.getApkLocPath();
                if (!StringUtils.isNullOrEmpty(apkLocPath)){
                    File file=new File(apkLocPath);
                    if (file.exists()){
                        if (!file.delete())
                            return flag;
                    }
                }
                if (appVersionMapper.deleteAppVersion(appVersion.getId())==1){
                    flag=true;
                }
            }
        }
        AppInfo appInfo = appInfoMapper.getAppInfo(id);
        if (!StringUtils.isNullOrEmpty(appInfo.getLogoLocPath())){
            File file=new File(appInfo.getLogoLocPath());
            if (file.exists()){
                if (!file.delete())
                    return false;
            }
        }
        if (appInfoMapper.deleteAppInfo(id)==1){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean updateAppStatus(AppInfo appInfo) {
        Integer devUserId=appInfo.getModifyBy();
        AppInfo info = appInfoMapper.getAppInfo(appInfo.getId());
        if (null==info){
            return false;
        }else {
            switch (info.getStatus()){
                //审核通过  上架
                case 2:
                    onSale(info,devUserId,4,2);
                    break;
                //已上架   下架
                case 4:
                    offSale(info,devUserId,5,1);
                    break;

                //已下架  上架
                case 5:
                    onSale(info,devUserId,4,2);
                    break;

                default:
                    return false;
            }
        }
        return true;
    }

    @Override
    public boolean updateStatus(Integer status, Integer id) {
        if (appInfoMapper.updateStatus(status,id)==1){
            return true;
        }
        return false;
    }

    //上架操作
    public void onSale(AppInfo appInfo,Integer devUserid,Integer appInfStatus,Integer versionStatus){
        AppInfo _appInfo=new AppInfo();
        _appInfo.setId(appInfo.getId());
        _appInfo.setModifyBy(devUserid);
        _appInfo.setOnSaleDate(new Date());
        _appInfo.setStatus(appInfStatus);
        appInfoMapper.updateAppInfo(_appInfo);

        AppVersion appVersion=new AppVersion();
        appVersion.setId(appInfo.getVersionId());
        appVersion.setPublishStatus(versionStatus);
        appVersion.setModifyBy(devUserid);
        appVersion.setModifyDate(new Date());
        appVersionMapper.updateAppVersion(appVersion);
    }

    //下架操作
    public void offSale(AppInfo appInfo,Integer devUserid,Integer appInfStatus,Integer versionStatus){
        AppInfo _appInfo=new AppInfo();
        _appInfo.setId(appInfo.getId());
        _appInfo.setModifyBy(devUserid);
        _appInfo.setOffSaleDate(new Date());
        _appInfo.setStatus(appInfStatus);
        appInfoMapper.updateAppInfo(_appInfo);

        AppVersion appVersion=new AppVersion();
        appVersion.setId(appInfo.getVersionId());
        appVersion.setPublishStatus(versionStatus);
        appVersion.setModifyBy(devUserid);
        appVersion.setModifyDate(new Date());
        appVersionMapper.updateAppVersion(appVersion);
    }
}
